/**
 * 
 */
package com.yuefresh.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.joda.time.DateTime;
import org.springframework.context.i18n.LocaleContextHolder;

/**
 * @author David Yang
 * 
 */
public class DateUtil {
	private static Log log = LogFactory.getLog(DateUtil.class);

	private static String datePattern = "MM/dd/yyyy";
	private static String cnDatePattern = "yyyy-MM-dd";

	public static String[][] dateString = {
			{"1","一月"},
			{"2","二月"},
			{"3","三月"},
			{"4","四月"},
			{"5","五月"},
			{"6","六月"},
			{"7","七月"},
			{"8","八月"},
			{"9","九月"},
			{"10","十月"},
			{"11","十一月"},
			{"12","十二月"}
	};
	// ~ Methods
	// ================================================================

	/**
	 * Return default datePattern (MM/dd/yyyy)
	 * 
	 * @return a string representing the date pattern on the UI
	 */
	public static String getDatePattern() {
		Locale locale = LocaleContextHolder.getLocale();
		if (locale.toString().equals("zh_CN")) {
			datePattern = cnDatePattern;
		} else {
			datePattern = cnDatePattern;
		}
		return datePattern;
	}

	public static String getDateTimePattern() {
		return DateUtil.getDatePattern() + " HH:mm:ss.S";
	}

	/**
	 * This method generates a string representation of a date's date/time in
	 * the format you specify on input
	 * 
	 * @param mask
	 *            the date pattern the string is in
	 * @param date
	 *            a date object
	 * @return a formatted string representation of the date
	 * 
	 * @see java.text.SimpleDateFormat
	 */
	public static final String convert(String mask, Date date) {
		SimpleDateFormat df = null;
		String returnValue = "";
		if (date == null) {
			log.error("aDate is null!");
		} else {
			df = new SimpleDateFormat(mask);
			returnValue = df.format(date);
		}
		return (returnValue);
	}

	/**
	 * This method generates a string representation of a date/time in the
	 * format you specify on input
	 * 
	 * @param mask
	 *            the date pattern the string is in
	 * @param strDate
	 *            a string representation of a date
	 * @return a converted Date object
	 * @see java.text.SimpleDateFormat
	 * @throws ParseException
	 */
	public static final Date convert(String mask, String strDate) {
		SimpleDateFormat df = null;
		Date date = null;
		df = new SimpleDateFormat(mask);

		if (log.isDebugEnabled()) {
			log.debug("converting '" + strDate + "' to date with mask '" + mask + "'");
		}
		try {
			date = df.parse(strDate);
		} catch (ParseException pe) {
			log.error("ParseException: " + pe);
			// throw new ParseException(pe.getMessage(), pe.getErrorOffset());
		}
		return date;
	}

	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
	/**
	 * Get date by interval days and system date
	 * 
	 * @param intervalDays
	 * @return
	 */
	public static Date getDate(int intervalDays) {
		Date sysDate = new Date();
		DateTime dateTime = new DateTime(sysDate);
		DateTime date = dateTime.plusDays(intervalDays);
		return date.toDate();
	}

	/**
	 * millisecond level
	 */
	public static long getIntervalTime(Date startDate, Date endDate) {
		long intervalTime = endDate.getTime() - startDate.getTime();
		return intervalTime;
	}
	
	/**
	 * day level
	 */
	public static long getIntervalDays(Date startDate, Date endDate) {
		java.util.Calendar calst = java.util.Calendar.getInstance();
		java.util.Calendar caled = java.util.Calendar.getInstance();
		calst.setTime(startDate);
		caled.setTime(endDate);
		// 设置时间为0时
		calst.set(java.util.Calendar.HOUR_OF_DAY, 0);
		calst.set(java.util.Calendar.MINUTE, 0);
		calst.set(java.util.Calendar.SECOND, 0);
		caled.set(java.util.Calendar.HOUR_OF_DAY, 0);
		caled.set(java.util.Calendar.MINUTE, 0);
		caled.set(java.util.Calendar.SECOND, 0);
		// 得到两个日期相差的天数
		long intervalDay = ((long) (caled.getTime().getTime() / 1000) - (long) (calst
				.getTime().getTime() / 1000)) / 3600 / 24;
		//int intervalDay = endDate.getDate() - startDate.getDate();
		return intervalDay;
	}

	/**
	 * 获取月份
	 */
	public static String getMonth(Date date) {
		DateTime dateTime = new DateTime(date);
		int month = dateTime.getMonthOfYear();
		String strMonth = "";
		if (month < 10)  strMonth = "0" + month;
		else strMonth = "" + month;
		return strMonth;
	}
	
	/**
	 * 获取年份
	 */
	public static String getYear(Date date) {
		DateTime dateTime = new DateTime(date);
		int year = dateTime.getYear();
		String strYear = "" + year;
		return strYear;
	}
	
	/**
	 * 获取日期
	 */
	public static String getDay(Date date) {
		DateTime dateTime = new DateTime(date);
		int day = dateTime.getDayOfMonth();
		String strDay = "";
		if (day < 10)  strDay = "0" + day;
		else strDay = "" + day;
		return strDay;
	}
	
	/**
	 * 获取星期
	 */
	public static String getWeek(Date date) {
		DateTime dateTime = new DateTime(date);
		int week = dateTime.getWeekOfWeekyear();
		String strWeek = "";
		if (week < 10)  strWeek = "0" + week;
		else strWeek = "" + week;
		return strWeek;
	}
	
	public static int[] getRtnMon(int begin,int cycle){
		int mon = 0;
		mon = begin;
		int[] monthCycle = new int[cycle];
		for(int i=0;i<cycle;i++){
			if( mon <= 12 ){
				monthCycle[i] = mon;
			}else{
				mon = mon - 12;
				monthCycle[i] = mon;
			}
			mon = mon + 1;
		}
		return monthCycle;
	}
	
	public static void main(String[] args) {
//		Date date = new Date();
//		String strDate = DateUtil.convert(date);
//		System.out.println(strDate);
//		//test getTime
//		System.out.println("=================test getTime");
//		String time = DateUtil.getTime(date);
//		System.out.println(time);
//		String secTime = DateUtil.getSecTime(date);
//		System.out.println(secTime);
//		String minTime = DateUtil.getMinTime(date);
//		System.out.println(minTime);
//		//test getIntervalTime
//		System.out.println("=================test getIntervalTime");
//		long intervalTime = 0;
//		intervalTime = DateUtil.getIntervalTime("2007-11-07 07:20:30", "2007-11-08 08:00:30", "H");
//		System.out.println(intervalTime);
//		intervalTime = DateUtil.getIntervalTime("2007-11-07", "2007-11-08", "D");
//		System.out.println(intervalTime);
//		//test getDate By interval days
//		System.out.println("=================test getDate by interval days");
//		Date getDate = DateUtil.getDate(9);
//		System.out.println(getDate);
		
		//String p_periodTime = DateUtil.convert("yyyy-MM", DateUtil.getDate(0));
		//long d = DateUtil.getIntervalDays(DateUtil.convert(DateUtil.getDateTimePattern(), "2013-05-06 17:23:00.0"), DateUtil.convert(DateUtil.getDateTimePattern(), "2013-05-16 20:23:00.1"));
		//Date dd = DateUtil.convert(p_periodTime + "-01");
		String d = DateUtil.getYear(new Date());
		System.out.println(d);
		//System.out.println(dd);
	}
}
